/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.put.dcs_modeler.fileTypeDataLoader.dataObjects;

import java.io.IOException;
import java.util.Set;
import java.util.logging.Logger;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataNode;
import org.openide.loaders.DataObjectExistsException;
import org.openide.loaders.MultiDataObject;
import org.openide.loaders.MultiFileLoader;
import org.openide.nodes.CookieSet;
import org.openide.nodes.Node;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.text.DataEditorSupport;
import org.openide.windows.TopComponent;
import pl.put.dcs_modeler.computingObjectEditor.ComputingObjectEditorTopComponent;

public class DCSComputingObjectDataObject extends MultiDataObject
{

    public static Logger log = Logger.getLogger( DCSComputingObjectDataObject.class.getName() );
    
    public DCSComputingObjectDataObject( FileObject pf, MultiFileLoader loader ) throws DataObjectExistsException, IOException
    {
        super( pf, loader );
        CookieSet cookies = getCookieSet();
        cookies.add( ( Node.Cookie )DataEditorSupport.create( this, getPrimaryEntry(), cookies ) );
    }

    @Override
    protected Node createNodeDelegate()
    {
        return new DataNode( this, Children.LEAF, getLookup() );
    }

    @Override
    public Lookup getLookup()
    {
        return getCookieSet().getLookup();
    }

    @Override
    public boolean equals( Object obj )
    {
        if ( obj instanceof DCSComputingObjectDataObject )
        {
            if ( this.getPrimaryFile().getPath().equals( ( ( DCSComputingObjectDataObject )obj ).getPrimaryFile().getPath() ) )
            {
                return true;
            }
        }
        return false;
    }
    
    @Override
    public void setModified( boolean modified )
    {
        if ( modified )
        {
            getCookieSet().add( getSaveCookie() );
        }
        else
        {
            getCookieSet().remove( getSaveCookie() );
        }
        super.setModified( modified );
    }

    private SaveCookie getSaveCookie()
    {
        return new SaveCookie()
        {

            @Override
            public void save() throws IOException
            {
                // strasznie malo wydajne, ale nie wiem, jak pobrac okno dla tego DataObject i je zmusic do zapisu
                Set< TopComponent > set = TopComponent.getRegistry().getOpened();
                for ( TopComponent node : set )
                {
                    if ( ( node instanceof ComputingObjectEditorTopComponent ) && ( ( ComputingObjectEditorTopComponent )node ).isBelongToThisTopComponent( DCSComputingObjectDataObject.this ) )
                    {
                        ( ( ComputingObjectEditorTopComponent )node ).save();
                        setModified( false );
                    }
                }
            }
        };
    }
}
